﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DosciWordPlugin.Models;
using DosciWordPlugin.Common;

namespace DosciWordPlugin.Controls
{
    public partial class ReferenceItem : UserControl
    {
        public Color ColorUnselected = Color.Transparent;
        public Color ColorSelected = Color.FromArgb(247, 247, 247);
        public Color ColorBorder = Color.FromArgb(204, 204, 204);

        public event Action<ReferenceItem> ClickEvent;

        public ReferenceItem(ReferenceModel referenceModel)
        {
            InitializeComponent();
            this.ReferenceModel = referenceModel;
        }

        private ReferenceModel _referenceModel;
        public ReferenceModel ReferenceModel
        {
            get 
            {
                return _referenceModel;
            }
            set
            {
                _referenceModel = value;
                UpdateUI();
            }
        }

        public bool Selected { get; set; }
        public bool ShowBorderTop { get; set; }
        public bool ShowBorderBottom { get; set; }

        public void SetUsed(bool used) 
        {
            _referenceModel.IsUsed = used;
            picUsed.Visible = used;
        }

        public void UpdateUI() 
        {
            try
            {
                lbTitle.Text = _referenceModel.Title;
                lbYearAuthor.Text = string.Format("({0}) {1}", _referenceModel.Year, _referenceModel.Author);
                if (_referenceModel.IsUsed) picUsed.Visible = true;
                else picUsed.Visible = false;
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
        }

        private void ReferenceItem_Click(object sender, EventArgs e)
        {
            try
            {
                Selected = !Selected;
                this.BackColor = Selected ? ColorSelected : ColorUnselected;

                if (ClickEvent != null) ClickEvent(this);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
        }

        private void ReferenceItem_Paint(object sender, PaintEventArgs e)
        {
            if (ShowBorderTop || ShowBorderBottom)
            {
                ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle,
                    ColorBorder, 0, ButtonBorderStyle.Solid, //左边
                    ColorBorder, ShowBorderTop ? 1 : 0, ButtonBorderStyle.Solid, //上边
                    ColorBorder, 0, ButtonBorderStyle.Solid, //右边
                    ColorBorder, ShowBorderBottom ? 1 : 0, ButtonBorderStyle.Solid);//底边
            }
        }
    }
}
